package com.leyou.mq;

import com.leyou.constant.MQConstants;
import com.leyou.utils.SMS;
import com.leyou.utils.SendMessageHelper;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class SmsListener implements SMS {
    @Autowired
    private SendMessageHelper helper;

    @Override
    public void sendMessage(String phone, String code) {
        helper.sendMessage(phone, code);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MQConstants.Queue.SMS_VERIFY_CODE_QUEUE, durable = "true"),
            exchange = @Exchange(value = MQConstants.Exchange.SMS_EXCHANGE_NAME, type = ExchangeTypes.TOPIC),
            key = MQConstants.RoutingKey.VERIFY_CODE_KEY
    ))
    public void sendMessage(Map<String, String> map) {
        String phone = map.get("phone");
        String code = map.get("code");
        sendMessage(phone, code);
    }
}
